package h0826;

import java.util.ArrayList;
import java.util.List;

/**
  * @description 求子集
  * @author 不知名帅哥
  * @date 2024/8/26 22:10
  * @version 1.0
*/
public class Collections {
    public static void main(String[] args) {
        int[] candidates=new int[]{2,3,6,7};
        System.out.println(combinationSum(candidates));
    }
    public static List<List<Integer>> combinationSum(int[] candidates) {
        List<List<Integer>> result = new ArrayList<>();
        backtrack(result, new ArrayList<>(), candidates, 0);
        return result;
    }

    private static void backtrack(List<List<Integer>> result,List<Integer> tempList, int[] candidates,int start) {

            result.add(new ArrayList<>(tempList));

            for (int i = start; i < candidates.length; i++) {
                tempList.add(candidates[i]);
                backtrack(result, tempList, candidates, i+1);//递归
                tempList.remove(tempList.size()-1); //回溯
            }
    }
}
